//package com.weimob.mcloud.wechatwork.customer.job;
//
//import com.alibaba.dubbo.config.annotation.Reference;
//import com.alibaba.fastjson.JSON;
//import com.google.common.collect.Lists;
//import com.weimob.elasticjob.annotation.ElasticJob;
//import com.weimob.mcloud.wechatwork.core.base.SingleResponse;
//import com.weimob.mcloud.wechatwork.core.entity.relation.tag.DeleteCustomerTagTaskDTO;
//import com.weimob.mcloud.wechatwork.core.entity.relation.tag.TagDetail;
//import com.weimob.mcloud.wechatwork.core.enums.tag.ConditionTypeEnum;
//import com.weimob.mcloud.wechatwork.core.enums.tag.DeleteCustomerTagStatusEnum;
//import com.weimob.mcloud.wechatwork.core.service.tag.CustomerTagCoreService;
//import com.weimob.mcloud.wechatwork.core.service.tag.TagCoreService;
//import com.weimob.mcloud.wechatwork.core.service.tag.TagTaskCoreService;
//import com.weimob.mcloud.wechatwork.core.service.tag.request.SingleCustomerTagRequest;
//import com.weimob.mcloud.wechatwork.core.service.tag.request.SingleTagTaskRequest;
//import com.weimob.soa.common.response.SoaResponse;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.collections.CollectionUtils;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
//import org.springframework.stereotype.Component;
//
//import javax.annotation.Resource;
//import java.util.HashSet;
//import java.util.List;
//import java.util.Set;
//
//@Slf4j
//@Component
//public class CustomerTagJob {
//
//    @Reference
//    private TagCoreService tagCoreService;
//
//    @Reference
//    private TagTaskCoreService tagTaskCoreService;
//
//    @Reference
//    private CustomerTagCoreService customerTagCoreService;
//
//    @Value("${batch.delete.customer.tag.size:30}")
//    private Integer deleteCustomerTagSize;
//
//    @Resource(name = "deleteCustomerTagByConditionThreadPool")
//    private ThreadPoolTaskExecutor deleteCustomerTagByConditionThreadPool;
//
//    @ElasticJob(name = "deleteCustomerTagByTag", cron = "0 0 2 * * ?")
//    public void deleteCustomerTagByTag() {
//
//        log.info("删除客户标签任务开始");
//
//        //标签
//        SingleTagTaskRequest<DeleteCustomerTagTaskDTO> tagRequest = new SingleTagTaskRequest<>();
//        tagRequest.setConditionType(ConditionTypeEnum.TAG.getType());
//        tagRequest.setStatus(DeleteCustomerTagStatusEnum.INIT.getStatus());
//        SoaResponse<SingleResponse<List<DeleteCustomerTagTaskDTO>>, Void> tagSoaResponse =
//                tagTaskCoreService.fetchDeleteCustomerTagTask(tagRequest);
//        log.info("查询删除标签任务, response:{}", JSON.toJSONString(tagSoaResponse));
//
//        if (0L == Long.parseLong(tagSoaResponse.getReturnCode())) {
//            SingleResponse<List<DeleteCustomerTagTaskDTO>> responseVo = tagSoaResponse.getResponseVo();
//            if (null != responseVo.getData()) {
//                deleteCustomerTag(toConditionIds(responseVo.getData()), ConditionTypeEnum.TAG.getType());
//            }
//        } else {
//            log.error("删除客户标签任务执行失败, request:{}, error:{}", JSON.toJSONString(tagRequest), tagSoaResponse.getReturnMsg());
//        }
//
//
//        //标签组
//        SingleTagTaskRequest<DeleteCustomerTagTaskDTO> tagGroupRequest = new SingleTagTaskRequest<>();
//        tagGroupRequest.setConditionType(ConditionTypeEnum.GROUP.getType());
//        tagGroupRequest.setStatus(DeleteCustomerTagStatusEnum.INIT.getStatus());
//        SoaResponse<SingleResponse<List<DeleteCustomerTagTaskDTO>>, Void> tagGroupSoaResponse =
//                tagTaskCoreService.fetchDeleteCustomerTagTask(tagGroupRequest);
//        log.info("查询删除标签组任务, response:{}", JSON.toJSONString(tagGroupSoaResponse));
//
//        if (0L == Long.parseLong(tagGroupSoaResponse.getReturnCode())) {
//            SingleResponse<List<DeleteCustomerTagTaskDTO>> responseVo = tagGroupSoaResponse.getResponseVo();
//            if (null != responseVo.getData()) {
//                deleteCustomerTag(toConditionIds(responseVo.getData()), ConditionTypeEnum.GROUP.getType());
//            }
//        } else {
//            log.error("删除客户标签组任务执行失败, request:{}, error:{}", JSON.toJSONString(tagRequest), tagGroupSoaResponse.getReturnMsg());
//        }
//
//    }
//
//    private void deleteCustomerTag(Set<String> conditionIdList, Integer conditionType) {
//        if (CollectionUtils.isEmpty(conditionIdList) || null == conditionType) {
//            return;
//        }
//
//        List<List<String>> partition = Lists.partition(Lists.newArrayList(conditionIdList), deleteCustomerTagSize);
//        for (List<String> idLis : partition) {
//            deleteCustomerTagByConditionThreadPool.execute(()->{
//                SingleCustomerTagRequest<TagDetail> request = new SingleCustomerTagRequest<>();
//                if (ConditionTypeEnum.TAG.getType().equals(conditionType)) {
//                    request.setTagIdList(idLis);
//                } else {
//                    request.setTagGroupIdList(idLis);
//                }
//                SoaResponse<SingleResponse<Boolean>, Void> soaResponse = customerTagCoreService.deleteCustomerTagByTag(request);
//                if (0L != Long.parseLong(soaResponse.getReturnCode())) {
//                    log.error("删除客户标签失败, request:{}, error:{}", JSON.toJSONString(request), soaResponse.getReturnMsg());
//                }
//            });
//
//        }
//    }
//
//    private Set<String> toConditionIds(List<DeleteCustomerTagTaskDTO> list) {
//        if (CollectionUtils.isEmpty(list)) {
//            return new HashSet<>();
//        }
//
//        Set<String> conditionIds = new HashSet<>();
//        for (DeleteCustomerTagTaskDTO tagTaskDTO : list) {
//            conditionIds.add(tagTaskDTO.getConditionId());
//        }
//
//        return conditionIds;
//    }
//}
